#!/bin/bash

# 已有数据库的启动脚本
echo "🚀 启动停车场管理系统 (已有数据库版)"
echo "=================================="

# 检查Java版本
echo "🔍 检查Java版本..."
JAVA_VERSION=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
echo "当前Java版本: $JAVA_VERSION"

if [[ "$JAVA_VERSION" != "17"* ]]; then
    echo "❌ 需要Java 17，当前版本: $JAVA_VERSION"
    echo "💡 运行 ./switch-to-java17.sh 切换Java版本"
    exit 1
fi

# 检查Redis
echo "🔍 检查Redis服务..."
if command -v redis-cli &> /dev/null; then
    if redis-cli ping 2>/dev/null | grep -q "PONG"; then
        echo "✅ Redis服务运行正常"
    else
        echo "⚠️  Redis服务未启动，正在启动..."
        if command -v brew &> /dev/null; then
            brew services start redis 2>/dev/null || echo "❌ 启动Redis失败，请手动启动"
        else
            echo "❌ 请手动启动Redis服务"
        fi
    fi
else
    echo "⚠️  Redis未安装，跳过Redis检查..."
fi

# 构建项目
echo "🔨 构建项目..."
mvn clean install -DskipTests

if [ $? -ne 0 ]; then
    echo "❌ 项目构建失败"
    echo "💡 检查项目配置和依赖"
    exit 1
fi

# 启动应用
echo "🎯 启动应用..."
echo "应用将在: http://localhost:8080"
echo "默认账号: admin / admin123"
echo "按 Ctrl+C 停止应用"
echo "=================================="

# 启动Spring Boot应用
mvn spring-boot:run -Dspring-boot.run.profiles=dev -pl park-admin